package xrz.cuanbao.adapters;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.xutils.x;
import org.xutils.common.util.LogUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.widget.TextView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;
import xrz.cuanbao.R;
import xrz.cuanbao.entitys.Comment;
import xrz.cuanbao.utils.DateKit;
import xrz.cuanbao.views.EmjoyUtils;
import xrz.cuanbao.views.RoundImageView;

public class Pinglun_Adapter extends QuickAdapter<Comment> {

	final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	// @Override
	// public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
	// ViewGroup arg4) {
	// if(arg3 == null){
	// arg3 = m_Inflater.inflate(R.layout.item_ping_childview, null);
	// }
	// TextView tv_name = (TextView) arg3.findViewById(R.id.tv_name);
	// TextView tv_content = (TextView) arg3.findViewById(R.id.tv_content);
	// return arg3;
	// }

	// @Override
	// public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup
	// arg3) {
	// if(arg2 == null){
	// arg2 = m_Inflater.inflate(R.layout.layout_pinglun_groupview, null);
	// }
	// return arg2;
	// }

	public Pinglun_Adapter(Context context, int layoutResId) {
		super(context, layoutResId);
	}

	@Override
	protected void convert(BaseAdapterHelper helper, Comment arg1) {
		RoundImageView imageView = helper.getView(R.id.img_photo);
		x.image().bind(imageView, arg1.avatar);
		String time = arg1.addtime;
		try {
			Date date = format.parse(time);
			time = DateKit.friendlyFormat(date);
		} catch (Exception e) {
			time = DateKit.friendlyFormat(new Date(Long.parseLong(time)));
		}
		helper.setText(R.id.tv_name, arg1.nickname).setText(R.id.tv_time, time);
		
		TextView tv_content = helper.getView(R.id.tv_content);
		tv_content.setText("");
		String str = arg1.content;
		if(str.contains("[") && str.contains("]")){
			String [] strs = str.split("\\[");
			for(int i=0;i<strs.length;i++){
				String s = strs[i];
				if(s.equals("") == false && s.contains("]")){
					SpannableString ssb = new SpannableString("["+s);  
					String resName = s.substring(0, s.indexOf("]"));
					int resid = EmjoyUtils.getResIdByName(resName);
					if(resid == -1){
						tv_content.append("["+s);
					}else{
						Drawable dd = context.getResources().getDrawable(resid);
						dd.setBounds(0, 0, dd.getIntrinsicWidth(), dd.getIntrinsicHeight());
						ImageSpan is = new ImageSpan(dd, ImageSpan.ALIGN_BASELINE);
						ssb.setSpan(is, 0, s.indexOf("]")+2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
						tv_content.append(ssb);
					}
				}else{
					tv_content.append(s);
				}
			}
		}else{
			tv_content.setText(str);
		}
		
	}

}
